(function($){
    $.submitAddPage = function(to) {
        $("#add-page").validate({
            errorClass: 'form-error',
            errorElement: 'div',
            submitHandler: function(form) {
                $(form).ajaxSubmit({
                    dataType: 'html',
                    success: function(responseText, statusText) {
                        if (responseText == "1") {
                            var data = {
                                action : 'get_last_page_url',
                                type : to
                            };

                            var send = $.param(data);
                            $.ajax({
                                url : 'ajax/pages-ajax.php',
                                data : send,
                                type : 'post',
                                dataType : 'html',
                                success : function(data) {
                                    window.location = data;
                                }
                            });
                        }
                        else {
                            $(".ajax-message").htmlTimer(responseText, 5000);
                        }
                    }
                });
            }
        });
    };
})(jQuery);

$(function(){

    $("#advance-toggle").click(function() {
        if ($('body').hasClass('effect')) {
            $("#advance-container").toggle("normal");
        }
        else {
            $("#advance-container").toggle();
        }
    });

    $(".page-ajax-submit-view").click(function(){
        $.submitAddPage('view');
    });

    $(".page-ajax-submit-edit").click(function(){
        $.submitAddPage('edit');
    });

    $(".cancel-submit").click(function(){
        CKEDITOR.instances.page_content.setData($("page_content").val());
        $("#advance-container").hide("normal");
        return true;
    });
});
